package com.smartview.jboss.spring.mvc;


import java.io.IOException;
import java.util.Date;
import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.ModelAndView;


import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.example.GraphPublisherExample;
import com.smartview.jboss.spring.dao.LoginDao;
import com.smartview.jboss.spring.dao.UserDao;
import com.smartview.jboss.spring.dto.LoginDTO;
import com.smartview.jboss.spring.dto.UserDTO;


@Controller
public class MainService {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private LoginDao loginDao;
	
	
	@RequestMapping(value = "/main/logout")
	public void init(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// I want to destroy the session here
		request.getSession().invalidate();
		
		// I want to go back to the main page
		response.sendRedirect("/smartView/pages/login_security.jsp");
	}

	/**
	 * * Ajax - @ResponseBody
	 * * @param request 
	 * * @return 
	 * * @throws
	 * Exception
	 */
	@RequestMapping("/main/ajaxValidateId")
	public @ResponseBody String validateId(HttpServletRequest request) throws Exception {
		
		String id = (request.getParameter("userid")!=null)?request.getParameter("userid"):"";
		String str="<font color='red'>You can not use the ID. Try by another ID</font>";
		

		
		UserDTO vo = new UserDTO();
		
		try{
		vo = userDao.getCheckUser(id);
		
		}catch(Exception e){
			str = "<font color='blue'>You can use the ID</font>";
			
		}
		
		if(id=="")
			str = "<font color='red'>No Empty.</font>";
				
		return str;
	}
	
	/**
	 * * Ajax - @ResponseBody
	 * * @param request 
	 * * @return 
	 * * @throws
	 * Exception
	 */
	@RequestMapping("/main/ajaxValidatePw")
	public @ResponseBody String validatePw(HttpServletRequest request) throws Exception {
		String pw = (request.getParameter("userpw")!=null)?request.getParameter("userpw"):"";
		String str="<font color='red'>Use only 5~8 characters. Try again</font>";

		if(pw.length()>=5 && pw.length()<=8)
			str = "<font color='blue'>OK</font>";
					
		return str;
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * @author yj
	 */
	@RequestMapping("/main/register")
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response) throws Exception{
		// SetView
		ModelAndView mav = new ModelAndView("main/registerComplete");
		
		LoginDTO loginDto = new LoginDTO();
		UserDTO userDto = new UserDTO();
		
		String id = (request.getParameter("userid")!=null)?request.getParameter("userid"):"";
		String pw = (request.getParameter("userpassword")!=null)?request.getParameter("userpassword"):"";
		String name = (request.getParameter("username")!=null)?request.getParameter("username"):"";
		String yahooWeatherChk = (request.getParameter("yahooWeatherChk")!=null)?request.getParameter("yahooWeatherChk"):"";
		String yahooNewsChk = (request.getParameter("yahooNewsChk")!=null)?request.getParameter("yahooNewsChk"):"";
		String yahooChk = (request.getParameter("yahooChk")!=null)?request.getParameter("yahooChk"):"";
		String facebookChk = (request.getParameter("facebookChk")!=null)?request.getParameter("facebookChk"):"";
		String twitterChk = (request.getParameter("twitterChk")!=null)?request.getParameter("twitterChk"):"";
		String zip = "", service="", facebookId, facebookPw, twitterId, twitterPw;
		
		userDto.setId(id);
		userDto.setPassword(pw);
		userDto.setName(name);
		userDto.setAuthority("ROLE_USER");
		userDto.setEnable("1");
		userDao.saveAccount(userDto);
		
		if(yahooWeatherChk.equals("on")){
			zip = (request.getParameter("yahooZip")!=null)?request.getParameter("yahooZip"):"";
			
			loginDto.setId(id);
			loginDto.setService_code("0");
			loginDto.setService_id(zip);
			loginDto.setService_pw("0");
			loginDao.saveAccount(loginDto);
		}
		
		if(yahooNewsChk.equals("on")){
			service = "0";
			
			loginDto.setId(id);
			loginDto.setService_code("3");
			loginDto.setService_id(service);
			loginDto.setService_pw("0");
			loginDao.saveAccount(loginDto);
		}
		
		if(yahooChk.equals("on")){
			service = (request.getParameter("yahooService")!=null)?request.getParameter("yahooService"):"";
			
			loginDto.setId(id);
			loginDto.setService_code("4");
			loginDto.setService_id(service);
			loginDto.setService_pw("0");
			loginDao.saveAccount(loginDto);
		}
		
		
		if(facebookChk.equals("on")){
			facebookId = (request.getParameter("facebookId")!=null)?request.getParameter("facebookId"):"";
			facebookPw = (request.getParameter("facebookPw")!=null)?request.getParameter("facebookPw"):"";
			
			loginDto.setId(id);
			loginDto.setService_code("1");
			loginDto.setService_id(facebookId);
			loginDto.setService_pw(facebookPw);
			loginDao.saveAccount(loginDto);
		}
		
		if(twitterChk.equals("on")){
			twitterId = (request.getParameter("twitterId")!=null)?request.getParameter("twitterId"):"";
			twitterPw = (request.getParameter("twitterPw")!=null)?request.getParameter("twitterPw"):"";
			
			loginDto.setId(id);
			loginDto.setService_code("2");
			loginDto.setService_id(twitterId);
			loginDto.setService_pw(twitterPw);
			loginDao.saveAccount(loginDto);
		}
		
		mav.addObject("id", id);
		
		return mav;
	}
	
	@RequestMapping("/main/facebook")
	public ModelAndView getFacebookData(HttpServletRequest request, HttpServletResponse response) throws Exception{
		// SetView

		return null;
	}
	
	@RequestMapping(value = "/deleteService")
	public void deleteService(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// I want to destroy the session here
		String id = SecurityContextHolder.getContext().getAuthentication().getName();
		String service_code = (request.getParameter("service_code")!=null)?request.getParameter("service_code"):"";
		loginDao.deleteService(id, service_code);
		
		// I want to go back to the main page
		response.sendRedirect("/smartView/spring/news/main");
	}
	
	@RequestMapping(value = "/addService")
	public void addService(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String id = SecurityContextHolder.getContext().getAuthentication().getName();
		String service_code = (request.getParameter("service_code")!=null)?request.getParameter("service_code"):"";
		String service_id = (request.getParameter("service_id")!=null)?request.getParameter("service_id"):"";
		String service_pw = (request.getParameter("service_pw")!=null)?request.getParameter("service_pw"):"";
		
		LoginDTO dto = new LoginDTO();
		dto.setId(id);
		dto.setService_code(service_code);
		dto.setService_id(service_id);
		dto.setService_pw(service_pw);
		
		loginDao.saveAccount(dto);
		
		// I want to go back to the main page
		response.sendRedirect("/smartView/spring/news/main");
	}

}
